/**
 * 观察者模式（一对多）
 * 主题和观察者是关联的，主题状态改变时，观察者会收到通知并自动更新
 */
class Subject {
  constructor() {
    this.observers = [];
  }
  addObserver(observer) {
    this.observers.push(observer);
  }
  removeObserver(observer) {
    this.observers = this.observers.filter(o => o !== observer);
  }
  notify(message) {
    this.observers.forEach(observer => observer.update(message));
  }
}
class Observer {
  constructor(name) {
    this.name = name;
  }
  update(message) {
    console.log(`${this.name}， ${message}`);
  }
}
const subject = new Subject();
const observer1 = new Observer('observer1');
const observer2 = new Observer('observer2');
subject.addObserver(observer1);
subject.addObserver(observer2);
subject.notify('Hello');